package study.rpc.dubbo.provider;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.container.Container;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;

@Slf4j
public class Startup {

    private static final ExtensionLoader<Container> LOADER = ExtensionLoader.getExtensionLoader(Container.class);

    private static volatile boolean running = true;

    public static void main(String[] args) {
        //
       // System.setProperty("zookeeper.sasl.client", "false");
        System.setProperty("nacos.logging.default.config.enabled","false");
        System.setProperty("dubbo.spring.config", "classpath:spring/spring-application.xml");
        //
        try {
            List<Container> containers = Arrays.asList(
                    //LOADER.getExtension("log4j"),
                    LOADER.getExtension("spring"));
            for (Container container : containers) {
                container.start();
                log.info("****** {} ******", container.getClass().getSimpleName());
            }
            log.info(">>>>>>>>>> Dubbo Started >>>>>>>>>>");
        } catch (Exception ex) {
            log.error("dubbo startup error, exit app!", ex);
            System.exit(1);
        }

        //死锁等待
        synchronized (Startup.class) {
            while (running) {
                try {
                    Startup.class.wait();
                } catch (Exception ex) {
                    System.exit(1);
                }
            }
        }
    }

    public static void main2(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-application.xml");
        context.start();

        //挂起主线程，防止退出
        new CountDownLatch(1).await();
    }
}
